
include( "shared.lua" )

ENT.AutomaticFrameAdvance = true 

function ENT:Initialize()
	self.BuggyModel = ClientsideModel( "models/buggy.mdl" )
	self.BuggyModel:SetNoDraw( true )
	self.NextSpin = 0
	self.LastParam = 0
end

function ENT:Draw()
	if CurTime() > self.NextSpin then
		if self:GetVelocity():Length() > 5 then
			local sequence = self.BuggyModel:LookupSequence("spin")
			self.BuggyModel:ResetSequence(sequence)
			self.NextSpin = CurTime() + self.BuggyModel:SequenceDuration()-0.05
		end
	end
	
	if self:GetVelocity():Length() <= 5 then
		local sequence = self.BuggyModel:LookupSequence("idle")
		self.BuggyModel:ResetSequence(sequence)
	end
	
	if self:GetOwner():KeyDown( IN_MOVELEFT ) then
		self.LastParam = math.Approach( self.LastParam, -1, FrameTime()*5 )
	elseif self:GetOwner():KeyDown( IN_MOVERIGHT ) then
		self.LastParam = math.Approach( self.LastParam, 1, FrameTime()*5 )
	else
		self.LastParam = math.Approach( self.LastParam, 0, FrameTime()*5 )
	end
	self.BuggyModel:SetPoseParameter( "vehicle_steer", self.LastParam ) 
	
	self.BuggyModel:SetPos( self:GetPos() )
	self.BuggyModel:SetModelScale( Vector( 0.15, 0.15, 0.15 ) )
	self:SetModelScale( Vector( 0.15,0.15,0.15 ) )
	//self:SetRenderAngles( Angle( 0,0,0 ) )
	local buggyAng = self:GetAngles()
	buggyAng:RotateAroundAxis( buggyAng:Up(), 270 )
	self.BuggyModel:SetRenderAngles( buggyAng )
	self.BuggyModel:SetRenderOrigin( self:GetPos() - buggyAng:Up()*12 )
	if( self:GetOwner():Alive() ) then
		self.BuggyModel:DrawModel()
	end
end
